package com.by.cloud.controller;

import cn.hutool.core.collection.CollUtil;
import com.by.cloud.annotation.PreAuthorize;
import com.by.cloud.common.BaseResponse;
import com.by.cloud.common.ResultUtils;
import com.by.cloud.enums.UserRoleEnum;
import com.by.cloud.exception.ErrorCode;
import com.by.cloud.exception.ThrowUtils;
import com.by.cloud.model.vo.tag.TagVo;
import com.by.cloud.service.TagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lzh
 */
@RestController
@RequestMapping("/tag")
@Api(tags = "标签模块")
public class TagController {

    @Resource
    private TagService tagService;

    @ApiOperation("批量新增标签")
    @PreAuthorize(role = UserRoleEnum.ADMIN)
    @PostMapping("/add")
    public BaseResponse<Boolean> addBatchTags(@RequestParam("tagList") List<String> tagNameList) {
        ThrowUtils.throwIf(CollUtil.isEmpty(tagNameList), ErrorCode.PARAMS_ERROR);
        boolean isSuccess = tagService.addBatchTags(tagNameList);
        return ResultUtils.success(isSuccess);
    }

    @ApiOperation("查询所有标签")
    @GetMapping("/list")
    public BaseResponse<List<TagVo>> listTags() {
        List<TagVo> tagVoList = tagService.listTags();
        return ResultUtils.success(tagVoList);
    }

    @ApiOperation("批量删除标签")
    @PreAuthorize(role = UserRoleEnum.ADMIN)
    @DeleteMapping("/batch/delete")
    public BaseResponse<Boolean> deleteTagsByIds(@RequestParam("ids") List<Long> ids) {
        ThrowUtils.throwIf(CollUtil.isEmpty(ids), ErrorCode.PARAMS_ERROR);
        boolean isSuccess = tagService.removeBatchTags(ids);
        return ResultUtils.success(isSuccess);
    }

    @ApiOperation("根据图片id查询对应的所有标签")
    @GetMapping("/getTagsByPicId/{id}")
    public BaseResponse<List<TagVo>> getTagsByPictureId(@PathVariable("id") Long id) {
        List<TagVo> tagVoList = tagService.getTagsByPictureId(id);
        return ResultUtils.success(tagVoList);
    }
}
